 
 
 * Figure 4
 
 
 cd "$data"
use "dataset.dta", clear


keep country_year_ regulation voted turnout_r 
 
bysort country_year_: egen mean_voter = mean(regulation) if voted==1
bysort country_year_: egen mean_nonvoter = mean(regulation) if voted==0
gen proportion_nonvoter = 1 - turnout_r
gen mean_voter_w = mean_voter * turnout_r
gen mean_nonvoter_w = mean_nonvoter * proportion_nonvoter
drop regulation voted
duplicates drop 

gen outcome = mean_voter_w + mean_nonvoter_w
gen bias =  outcome - mean_voter
 
 
 
 cd "$data"
use "dataset.dta", clear

foreach var in selfplacement redistribution regulation spending samesexmarriage immigration environment laworder euintegration {

    preserve
    
    keep country_year_ `var' voted turnout_r 
    
    bysort country_year_: egen mean_voter = mean(`var') if voted==1
    bysort country_year_: egen mean_nonvoter = mean(`var') if voted==0
    
    gen proportion_nonvoter = 1 - turnout_r
    gen mean_voter_w = mean_voter * turnout_r
    gen mean_nonvoter_w = mean_nonvoter * proportion_nonvoter
    
    drop `var' voted
    duplicates drop 
    
	collapse (max) mean_voter mean_nonvoter turnout_r mean_voter_w mean_nonvoter_w, by(country_year_)
	
    gen outcome = mean_voter_w + mean_nonvoter_w
    gen bias = outcome - mean_voter
		 
    cd "$data/consequences"
    save "`var'.dta", replace
    
    restore
}

 
 foreach var in selfplacement redistribution regulation spending samesexmarriage immigration environment laworder euintegration {

 use "`var'.dta", clear 
 
 collapse (firstnm) turnout_r mean_voter mean_nonvoter bias ///
         mean_voter_w mean_nonvoter_w outcome, by(country_year_)

	 save "$data/consequences/`var'.dta", replace 	 
	 }
	 
	
 cd "$data/consequences"
 
 use "selfplacement.dta", clear 
 gen item = 1 
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/selfplacement.dta"
 save "selfplacement.dta", replace 
 
 use "redistribution.dta", clear 
 gen item = 2
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/redistribution.dta"
 save "redistribution.dta", replace 
 
 use "regulation.dta", clear 
 gen item = 3
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/regulation.dta"
 save "regulation.dta", replace 
 
 use "spending.dta", clear 
 gen item = 4
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/spending.dta" 
 save "spending.dta", replace 
 
 use "environment.dta", clear 
 gen item = 5
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/environment.dta" 
 save "environment.dta", replace 
  
 use "immigration.dta", clear 
 gen item = 6
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/immigration.dta"  
 save "immigration.dta", replace 
 
 use "samesexmarriage.dta", clear 
 gen item = 7
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/samesexmarriage.dta"  
 save "samesexmarriage.dta", replace 

 use "laworder.dta", clear 
 gen item = 8
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/laworder.dta"  
 save "laworder.dta", replace 
 
 use "euintegration.dta", clear 
 gen item = 9
 gen sample = country_year_
 merge 1:1 sample using "${data}/pvalues/euintegration.dta"   
 save "euintegration.dta", replace 
  
  
use "selfplacement.dta", clear 
append using "redistribution.dta" 
append using "regulation.dta" 
append using "spending.dta" 
append using "environment.dta" 
append using "immigration.dta" 
append using "samesexmarriage.dta" 
append using "laworder.dta" 
append using "euintegration.dta"


 label define itemlabels 1"left-right" 2"redistribution" 3"regulation" 4"spending" 5"environment" 6"immigration" 7"same-sex marriage" 8"civil liberties" 9"EU integration"
 label values item itemlabels 
 
 gen significant = .
replace significant = 1 if p_voted<0.050
replace significant = 0 if p_voted>0.049

save "consequences_merged.dta", replace 
	

	
	    dotplot bias, over(item) center xlabel(, angle(90) labsize(small)) xtitle("") yscale(range(-0.6(0.3)0.6)) ylabel(-0.6(0.3)0.6) yline(0, lpattern(dash) lcolor(black)) msize(4pt) jitter(2) ysize(6) xsize(8) colorvar(significant) colorrule(%25) colordiscrete coloruseplegend colorlist(gs12%50 black%70) ytitle("bias") 
		  graph save "${figures}/Figure4.gph", replace 
		  graph export "${figures}/Figure4.png", as(png) replace 

		  
		  
